package com.hedl.practice.diyJava;


import java.math.BigDecimal;

public class JavaAndBigDecimal {
    public static void main(String[] args) {
        /*BigDecimal bd = new BigDecimal("123.4567");
        BigDecimal bd2 = new BigDecimal("123.45");
        BigDecimal bd3 = new BigDecimal("123.4500");
        BigDecimal bd4 = new BigDecimal("1234500");

        System.out.println(bd.multiply(bd));        //multiply：类似于乘法；

        System.out.println(bd2.scale());        //scale:表示小数位数

        BigDecimal b3 = bd3.stripTrailingZeros();       //去掉末尾0
        System.out.println(bd3.scale());
        System.out.println("去掉末尾0的结果-->"+b3.scale());

        BigDecimal b4 = bd4.stripTrailingZeros();®
        System.out.println(bd4.scale());
        System.out.println("去掉末尾0的结果-->"+b4.scale());*/


        /*BigDecimal bd = new BigDecimal("123.456789");
        *//**
         * HALF_UP==四舍五入
         *//*
        BigDecimal bd1 = bd.setScale(4, RoundingMode.HALF_UP);      //四舍五入

        *//**
         * DOWN==保留小数点后面多少位
         *//*
        BigDecimal bd2 = bd.setScale(3,RoundingMode.DOWN);      //直接截断
        System.out.println("--->"+bd1);
        System.out.println("--->"+bd2);*/



        /*BigDecimal bd = new BigDecimal("123.456");
        BigDecimal bd2 = new BigDecimal("23.456789");
        BigDecimal bd3 = bd.divide(bd2,10,RoundingMode.HALF_UP);        //123.456%23.456=5.2631244626
        //BigDecimal bd4 = bd.divide(bd2);            //报错，异常，因为除不尽---》ArithmeticException
        System.out.println("---->"+bd3);
        //System.out.println("---->"+bd4);*/


        BigDecimal bd1 = new BigDecimal("12.345");
        BigDecimal bd2 = new BigDecimal("0.12");

        BigDecimal[] dr = bd1.divideAndRemainder(bd2);
        System.out.println(dr[0]);
        System.out.println(dr[1]);

        if (dr[1].signum() == 0){
            System.out.println("相等");
        }else {
            System.out.println("不相等");
        }

        System.out.println("-------------------");

        BigDecimal bd3 = new BigDecimal("123.456");
        BigDecimal bd4 = new BigDecimal("123.45600");
        System.out.println(bd3.equals(bd4));        //不相等
        System.out.println(bd3.equals(bd4.stripTrailingZeros()));       //stripTrailingZeros==删除末尾的0；bd4==123.456==bd3==123.456

        //-1、0或1，因为此BigDecimal在数值上小于、等于或大于
        System.out.println(bd3.compareTo(bd4));     //等于

    }
}
